package com.chc.web.user.handler.user;

import com.chc.enums.UserTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: chc
 * @Date: 2025/01/15/15:25
 * @Description: 工厂类
 */
@Component
public class UserTypeHandlerFactory implements InitializingBean {

    @Resource
    private List<LoginTypeHandler> loginTypeHandlerList;

    private Map<UserTypeEnum, LoginTypeHandler> userTypeMap = new HashMap<>();

    @Override
    public void afterPropertiesSet(){
        loginTypeHandlerList.forEach(loginTypeHandler ->
                userTypeMap.put(loginTypeHandler.getUserType(), loginTypeHandler)
        );
    }

    public LoginTypeHandler getUserTypeHandler(String userType){
        UserTypeEnum byCode = UserTypeEnum.getByCode(Integer.valueOf(userType));
        return userTypeMap.get(byCode);
    }
}
